<?php
/**
 *Image Model
 *Administers images and their database entries
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 *Class Image_Model
 *
 *@package model
 */
class Image_Model extends Model{
    /**
     * Holds the database reference
     * @var object
     */
    private $oDBInstance;
    
    /**
     * Model constructor
     * Establishes connection to database 
     */
    public function __construct(){
        /**
         * Gets the database reference from Mysql_dbconnection
         * @var object
         */
        $this->oDBInstance = Mysql_dbconnection::getInstance();

    }

    /**
     * Returns the title of the page with the given id
     * @param integer $iPage the page id of the page
     * @param integer $iLanguage the language of the title the that should be returned
     */
    public function getImageLink($iImageID){

        $iImageID = $this->oDBInstance->getSQLValueString($iImageID, 'int');
        $this->oDBInstance->performSelect('link', 'bs_image', "id = $iImageID");
        $aResult = $this->oDBInstance->fetch('array');

        return $aResult[0]['link'];
    }

    /**
     * Deltes a product image
     * @param integer image id
     */
    public function deleteProductImage($iImageID){

        $iImageID = $this->oDBInstance->getSQLValueString($iImageID, 'int');

        $this->oDBInstance->performSelect("id", 'bs_product', "mainimage = $iImageID");
        $aResult = $this->oDBInstance->fetch('array');

        if(isset($aResult[0]['id'])){
            $iProductID = $aResult[0]['id'];
            $this->oDBInstance->query("SELECT MIN(id) as id FROM bs_image WHERE product = $iProductID AND id != $iImageID");
            $aNextFirstImage = $this->oDBInstance->fetch('array');
            if(!isset($aNextFirstImage[0]['id'])){
                $this->oDBInstance->query("UPDATE bs_product SET mainimage = 1 WHERE id = $iProductID");
            }else{
                $iNewMainImage = $aNextFirstImage[0]['id'];
                $this->oDBInstance->query("UPDATE bs_product SET mainimage = $iNewMainImage WHERE id = $iProductID");
            }

        }
        //Deletes the image in the database
        return $this->oDBInstance->performDelete( "$iImageID",'bs_image','id');
    }

    /**
     * Saves an uploaded image in the database
     * @param integer product ID
     * @param string image name
     */
    public function uploadProductImage($iProductID, $sImageName){

        $iProductID = $this->oDBInstance->getSQLValueString($iProductID, 'int');

        $this->oDBInstance->query("INSERT INTO `bs_image` (
			`id` ,
			`link` ,
			`product`
			)VALUES (NULL , '$sImageName', '$iProductID');");

    }

    /**
     * Delivers the image ID
     */
    public function getImageID(){

        $this->oDBInstance->query("SELECT MAX(id) as id FROM `bs_image`");

        $aID = $this->oDBInstance->fetch('array');
        $iID = $aID[0]['id'];
        return $iID;
    }

}
?>